﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Promote
/// </summary>
public class Promote
{
    #region"Properties"
    public int MaKhuyenMai { get; set; }
    public DateTime NgayBDKM { get; set; }
    public DateTime NgayKTKM  { get; set; }
    public String TenSPKM { get; set; }
    public float GiamGia { get; set; }
    #endregion"End Properties"
    #region"Methods"
    public List<Promote> GetAllPromote()
    {
        try
        {
            List<Promote> list = new List<Promote>();
            String StoredProcedureName = "sp_GetAllPromote";
            String connectionString = DataAccess.ConnectionString;
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand comd = new SqlCommand(StoredProcedureName, con);
            con.Open();
            SqlDataReader datareader = comd.ExecuteReader(CommandBehavior.CloseConnection);
            if (datareader.HasRows)
            {
                while (datareader.Read())
                {
                    Promote p = new Promote()
                    {
                        MaKhuyenMai = int.Parse(datareader["MaKhuyenMai"].ToString()),
                        NgayBDKM = DateTime.Parse(datareader["NgayBDKM"].ToString()),
                        NgayKTKM = DateTime.Parse(datareader["NgayKTKM"].ToString()),
                        TenSPKM = datareader.GetString(4),
                        GiamGia = float.Parse(datareader["GiamGia"].ToString())

                    };
                    list.Add(p);
                }
            }
            return list;
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    public DataTable GetPromoteByID(int MaKhuyenMai)
    {
        try
        {
            String StoredProcedureName = "sp_GetPromoteByID";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@MaKhuyenMai",MaKhuyenMai.ToString());
            return DataAccess.GetList(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    public DataTable GetPromoteLapTop(int MaKhuyenMai)
    {
        try
        {
            String StoredProcedureName = "sp_GetPromoteLapTop";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@MaKhuyenMai", MaKhuyenMai.ToString());
            return DataAccess.GetList(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    public void InsertPromote(Promote p)
    {
        try
        {
            String StoredProcedureName = "sp_InsertPromote";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@MaKhuyenMai",p.MaKhuyenMai.ToString());
            d.Add("@NgayBDKM",p.NgayBDKM.ToShortDateString());
            d.Add("@NgayKTKM",p.NgayKTKM.ToShortDateString());
            d.Add("@TenSPKM",p.TenSPKM);
            d.Add("@GiamGia",p.GiamGia.ToString());
            DataAccess.ExecuteNonQuery(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    public void DeletePromote(Promote p)
    {
        try
        {
            String StoredProcedureName = "sp_DeletePromote";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@MaKhuyenMai",p.MaKhuyenMai.ToString());
            DataAccess.ExecuteNonQuery(StoredProcedureName, d);

        }
        catch (Exception e)
        {
            throw e;
        }
    }
    public void UpdatePromote(Promote p)
    {
        try
        {
            String StoredProcedureName = "sp_UpdatePromote";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@MaKhuyenMai",p.MaKhuyenMai.ToString());
            d.Add("@NgayBDKM",p.NgayBDKM.ToShortDateString());
            d.Add("@NgayKTKM",p.NgayKTKM.ToShortDateString());
            d.Add("@TenSP",p.TenSPKM);
            d.Add("@GiamGia",p.GiamGia.ToString());
            DataAccess.ExecuteNonQuery(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    #endregion"End Methods"

}
